﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.Utility;
using Sector7.Framework.Enums;
using Sector7.Framework.Core;
using Sector7.Framework.DataDictionary.Windows;
using System.ComponentModel;
using Sector7.Framework.Consts;
using Sector7.Framework.DataDictionary.Agents;

namespace Sector7.Framework.DataDictionary.Controls
{
    /// <summary>
    /// ValueDomainTabItem.xaml 的交互逻辑
    /// </summary>
    public partial class ValueDomainTabItem : UserControl
    {
        public static readonly DependencyProperty DataGroupProperty = DependencyProperty.Register("DataGroup", typeof(S7_DataGroup), typeof(ValueDomainTabItem));

        [Description("获取或设置数据分组")]
        [Category("其他")]
        public S7_DataGroup DataGroup
        {
            get { return this.GetValue(DataGroupProperty) as S7_DataGroup; }
            set { this.SetValue(DataGroupProperty, value); }
        }

        public ValueDomainTabItem()
        {
            InitializeComponent();
        }

        private List<S7_ValueDomain> m_valueDomains = null;

        private void GetData()
        {
            if (DataGroup != null)
            {
                lblGroupName.Content = DataGroup.GroupName;

                m_valueDomains = ValueDomainServiceAgent.GetAll(DataGroup.GroupId);
                
                ClientUtil.LoadItemsSource(dtgValueDomain, m_valueDomains);
            }
        }

        private void DoModify()
        {
            var selectedItem = dtgValueDomain.SelectedItem as S7_ValueDomain;

            if (selectedItem != null)
            {
                var modifyWindow = new AddValueDomainWindow();
                modifyWindow.DataGroup = DataGroup;
                modifyWindow.ValueDomain = selectedItem;
                modifyWindow.InitializeWindow(CrudType.Update);
                modifyWindow.AddSuccess += new EventHandler(addWindow_AddSuccess);

                modifyWindow.ShowDialog();
            }
        }

        private void DoDelete()
        {
            var selectedItem = dtgValueDomain.SelectedItem as S7_ValueDomain;
            if (selectedItem != null)
            {
                MessageBoxResult result = Alert.ShowDialog(AlertType.Question, "是否确认删除选中项？");

                if (result == MessageBoxResult.Yes)
                {
                    int optResult = ValueDomainServiceAgent.Delete(selectedItem.DomainId);

                    if (optResult == C_OperatorResultType.SUCCESS)
                    {
                        m_valueDomains.Remove(selectedItem);
                        dtgValueDomain.Items.Refresh();
                        ClientUtil.LoadItemsSource(dtgItems, new List<S7_ValueDomainItem>());
                    }
                }
            }
        }

        private void DoAddItem()
        {
            AddValueDomainItemWindow addItemWindow = new AddValueDomainItemWindow();
            addItemWindow.InitializeWindow(CrudType.Create);
            addItemWindow.AddSuccess += new EventHandler(addItemWindow_AddSuccess);

            addItemWindow.ShowDialog();
        }

        private void DoModifyItem()
        {
            object selectedItem = dtgItems.SelectedItem;

            if (selectedItem != null)
            {
                var modifyItemWindow = new AddValueDomainItemWindow();
                modifyItemWindow.ValueDomainItem = selectedItem as S7_ValueDomainItem;
                modifyItemWindow.InitializeWindow(CrudType.Update);
                modifyItemWindow.AddSuccess += new EventHandler(addItemWindow_AddSuccess);

                modifyItemWindow.ShowDialog();
            }
        }

        private void DoDeleteItem()
        {
            var selectedItem = dtgItems.SelectedItem as S7_ValueDomainItem;
            if (selectedItem != null)
            {
                MessageBoxResult result = Alert.ShowDialog(AlertType.Question, "是否确认删除选中项？");

                if (result == MessageBoxResult.Yes)
                {
                    int optResult = ValueDomainItemServiceAgent.Delete(selectedItem.DomainItemId);

                    if (optResult == C_OperatorResultType.SUCCESS)
                    {
                        var valueDomain = dtgValueDomain.SelectedItem as S7_ValueDomain;
                        valueDomain.Items.Remove(selectedItem);
                        dtgItems.Items.Refresh();
                    }
                }
            }
        }

        private void miAddItem_Click(object sender, RoutedEventArgs e)
        {
            DoAddItem();
        }

        private void miModifyItem_Click(object sender, RoutedEventArgs e)
        {
            DoModifyItem();
        }

        private void miDeleteItem_Click(object sender, RoutedEventArgs e)
        {
            DoDeleteItem();
        }

        private void btnAddValueDomain_Click(object sender, RoutedEventArgs e)
        {
            var addWindow = new AddValueDomainWindow();
            addWindow.DataGroup = DataGroup;
            addWindow.InitializeWindow(CrudType.Create);
            addWindow.AddSuccess += new EventHandler(addWindow_AddSuccess);

            addWindow.ShowDialog();
        }

        private void addWindow_AddSuccess(object sender, EventArgs e)
        {
            var addWindow = sender as AddValueDomainWindow;
            var newValueDomain = addWindow.ValueDomain;

            newValueDomain.Items = ValueDomainItemServiceAgent.GetAll(newValueDomain.DomainId);

            switch (addWindow.Crud)
            {
                case CrudType.Create:
                    m_valueDomains.Add(newValueDomain);

                    ClientUtil.LoadSelectedItem(dtgValueDomain, newValueDomain);

                    addWindow.ResetWindow();
                    break;
                case CrudType.Update:
                    ClientUtil.UpdateToItems(m_valueDomains, C_PrimaryKeyName.VALUE_DOMAIN, newValueDomain);
                    break;
                default:
                    break;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.IsVisible)
            {
                GetData();
            }
        }

        private void dtgValueDomain_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedValueDomain = dtgValueDomain.SelectedItem as S7_ValueDomain;

            if (selectedValueDomain != null)
            {
                ClientUtil.LoadItemsSource(dtgItems, selectedValueDomain.Items);
            }
        }

        private void dtgValueDomain_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DoModify();
        }

        private void dtgItems_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DoModifyItem();
        }

        private void addItemWindow_AddSuccess(object sender, EventArgs e)
        {
            var addItemWindow = sender as AddValueDomainItemWindow;
            S7_ValueDomain valueDomain = null;
            S7_ValueDomainItem newItem = addItemWindow.ValueDomainItem;

            switch (addItemWindow.Crud)
            {
                case CrudType.Create:
                    valueDomain = dtgValueDomain.SelectedItem as S7_ValueDomain;

                    if (valueDomain != null)
                    {
                        newItem.DomainId = valueDomain.DomainId;

                        newItem.DomainItemId = ValueDomainItemServiceAgent.Add(newItem);
                        
                        valueDomain.Items.Add(newItem);
                        ClientUtil.LoadSelectedItem(dtgItems, newItem);

                        addItemWindow.ResetWindow();
                    }
                    break;
                case CrudType.Update:
                    bool isChanged = false;
                    EntityDeltaBase delta = addItemWindow.GetDelta(C_PrimaryKeyName.VALUE_DOMAIN_ITEM, ref isChanged);

                    if (isChanged)
                    {
                        int result = ValueDomainItemServiceAgent.Set(delta);

                        if (result == C_OperatorResultType.SUCCESS)
                        {
                            newItem = addItemWindow.ValueDomainItem;

                            valueDomain = m_valueDomains.Find(delegate(S7_ValueDomain __valueDomain)
                            {
                                return __valueDomain.DomainId == newItem.DomainId;
                            });

                            if (valueDomain != null)
                            {
                                ClientUtil.UpdateToItems(valueDomain.Items, C_PrimaryKeyName.VALUE_DOMAIN_ITEM, newItem);
                            }
                        }
                    }
                    break;
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            DoDelete();
        }

        private void btnModify_Click(object sender, RoutedEventArgs e)
        {
            DoModify();
        }
    }
}
